package edu.hawaii.ics414.blackjack.player;

import edu.hawaii.ics414.blackjack.card.Card;
import edu.hawaii.ics414.blackjack.card.GameDeck;

/**
 * An AI that looks at the top card of the deck to determine what move to make.
 * 
 * @author Branden Ogata
 *
 */

public class CheaterPlayer extends ExperiencedPlayer
{
  /**
   * Creates a new CheaterPlayer.
   * 
   * @param money The int equal to the amount of money the new CheaterPlayer starts with.
   * 
   */
  
  public CheaterPlayer(int money)
  {
    super(money);
  }
  
  /**
   * Returns the move of this CheaterPlayer.
   * 
   * @param deck The GameDeck containing the cards to use.
   * @param hand The int indicating the hand to play on.
   * @param hole The Card that the dealer has showing.
   * 
   * @return action The Action that this CheaterPlayer intends to play.
   * 
   */

  
  @Override
  public Action getMove(GameDeck deck, int hand, Card hole)
  {
    Action action = super.getMove(deck, hand, hole);
    
    // If not splitting, then use knowledge of next card
    if (!action.equals(Action.SPLIT))
    {
      int sumAfterDraw = this.sum(hand) + deck.peek().getValue();
      
      // If the next card would lead to a bust, then stand
      if (sumAfterDraw > 21)
      {
        action = Action.STAND;
      }
      // Else if it is legal to double down and the next card would give a 20 or 21
      else if ((this.canDoubleDown(hand)) && ((sumAfterDraw == 20) || (sumAfterDraw == 21)))
      {
        action = Action.DOUBLE_DOWN;
      }
      // Else hit
      else
      {
        action = Action.HIT;
      }
    }
    
    return action;
  }

  /**
   * Starts a new hand with an initial bet based on the last round.
   * 
   * @param deck The GameDeck to take cards from.
   * 
   */
  
  @Override
  public void startHand(GameDeck deck)
  {
    // If won the last round and can bet more, increase bet by 5
    if ((this.lastRound > 0) && (this.lastBet[0] + 5 <= 100))
    {
      this.setBet(0, this.lastBet[0] + 5);
    }
    // Else if lost the last round and can bet less, decrease bet by 5
    else if ((this.lastRound < 0) && (this.lastBet[0] - 5 > 0))
    {
      this.setBet(0, this.lastBet[0] - 5);
    }
    // Else do not change the bet
    else
    {
      this.setBet(0, this.lastBet[0]);
    }
    
    super.startHand(deck);
  }  
  
  
  /**
   * Returns a String representation of the cards for this BeginnerPlayer.
   * This should show the cards face down.
   * 
   * @return desc A String representing the cards that this BeginnerPlayer has.
   * 
   */

  @Override
  public String getCards()
  {
    String desc = "";
    int i = 0;
    // For each hand
    for (Card[] hand : this.hands)
    {
      //prints the value and bet for the hand as well.
      if (hand[0] != null)
      {
        desc += "(Bet: ";
        desc += this.getBet(i);
        desc += ") ";
        desc += "Hand ";
        desc += (i + 1);
        desc += ": (Value: ";
        desc += handValue(i);
        desc += ")  -  ";
      }
      // Add each card to the String
      for (Card card : hand)
      {
        if (card != null)
        {
          desc += card + " ";
        }
      }
      
      if (hand[0] != null)
      {
        desc += "\n";
      }
      i++;
    }
    
    return desc;
  }

}
